package cajondesastre

import dao.Word
import groovy.sql.DataSet
import groovy.sql.Sql
import org.apache.log4j.Logger;
import java.util.regex.Pattern;


class meterexcludesAutomaticamente {
	
	static main(args) {
		process3()
	}
	
	static sql, sql2
	static Logger _log
	static void process(){ 
		
		_log = Logger.getLogger("Meter ids");
		_log.info "Empezamos"
		
		sql = groovy.sql.Sql.newInstance("jdbc:sqlite:" + utils.Configuration.getInstance().getProperty("sqlitePath"),"org.sqlite.JDBC")
				
		def id = 0
		Closure addId = { row ->
			try {
			Word wrd = new Word(row);
			
			id++
			_log.info wrd.word
			
			sql.execute("UPDATE Word set excluded = 1 where idword = ${wrd.idWord}")
			
			
			
			} catch (Exception e) {
				_log.error e
			}
		
		}
		
		_log.info "************************"
		_log.info ("Insertando ids")
		_log.info "************************"
	//	sql2.eachRow("select word from Word", addId)
		
		sql.rows("select * from Word where length(word) = 1").each(addId)
		
		sql.execute("update word set excluded = 1 where word = 'del'")
		sql.execute("update word set excluded = 1 where word = 'el'")
		sql.execute("update word set excluded = 1 where word = 'al'")
		sql.execute("update word set excluded = 1 where word = ''")
		sql.execute("update word set excluded = 1 where word = '&'")
		
		sql.execute("update word set structural = 1 where word = 'Univ'")
		sql.execute("update word set structural = 1 where word = 'Cent'")
		sql.execute("update word set structural = 1 where word = 'Hosp'")
		sql.execute("update word set structural = 1 where word = 'Dept'")
		sql.execute("update word set structural = 1 where word = 'Inst'")
		sql.execute("update word set structural = 1 where word = 'Fac'")
		sql.execute("update word set structural = 1 where word = 'Ctr'")
		sql.execute("update word set structural = 1 where word = 'Unidad'")
		sql.execute("update word set structural = 1 where word = 'Unit'")
		sql.execute("update word set structural = 1 where word = 'Sect'")
		sql.execute("update word set structural = 1 where word = 'Div'")
		sql.execute("update word set structural = 1 where word = 'Lab'")
		sql.execute("update word set structural = 1 where word = 'Grp'")
		sql.execute("update word set structural = 1 where word = 'Sch'")
		sql.execute("update word set structural = 1 where word = 'Dpto'")
		sql.execute("update word set structural = 1 where word = 'Unitat'")
		sql.execute("update word set structural = 1 where word = 'Escuela'")
		sql.execute("update word set structural = 1 where word = 'ETSI'")
		sql.execute("update word set structural = 1 where word = 'Dipartimento'")
		
		_log.info "fin"
	}
	
	static void process2(){
		
		_log = Logger.getLogger("Meter ids");
		_log.info "Empezamos"
		
		business.database.Database.getInstance().sqlnewcorps.rows("select corp from corps").each({
			
			String corp = it[0]
			String checkSql = "Select * from maescorp where corp = '${corp}'"
			if ((business.database.Database.getInstance().sqlmaescorp.firstRow(checkSql)) == null) {
				String ins = "insert into corp values ('${corp}')"
				business.database.Database.getInstance().sqlnewcorps.execute ins
			}
			
		})
		
		_log.info "fin"
	}
	
	static void process3(){
		
		_log = Logger.getLogger("Exportar de db a access");
		_log.info "Empezamos"
		
		business.database.Database.getInstance().sqlite.eachRow("select corp, iso, provincia, instit, organismo, dpto from corp co inner join centers_corps cc on co.idcorp = cc.idcorp inner join center ce on ce.idce = cc.idce where co.idcorp <= 297070" ,{
			
			String corp = it[0]
			String iso = it[1]
			String provincia = it[2]
			String instit = it[3]
			String organismo = it[4]
			String dpto = it[5]
			println "${corp}"
			String insSql = "insert into maescorp (corp, provincia, instit, organismo, dpto, iso) values ('${corp}', '${provincia}', '${instit}', '${organismo}', '${dpto}', '${iso}')"
			
			business.database.Database.getInstance().sqlmaescorp.execute(insSql)
				
				
		})
		
		_log.info "fin"
	}
}
